package com.zhongke.cotrun.controller;

import com.github.pagehelper.PageInfo;
import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.model.pojo.SysMessageEntity;
import com.zhongke.cotrun.model.pojo.SysMessageEntityVo;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.service.SysMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 系统消息
 *
 * @author lee
 * @email wawzj512541@gmail.com
 * @date 2020-03-30 09:58:29
 */
@RestController
@Api(tags = "系统消息模块")
@RequestMapping("/sysMessage")
public class SysMessageController {
    @Autowired
    private SysMessageService sysMessageService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "消息列表", httpMethod = "GET")
    public APIResponse list(SysMessageEntityVo entity,
                            @RequestParam(defaultValue = "1")@ApiParam(value = "当前页,默认为1", required = true) Integer pageNum,
                            @RequestParam(defaultValue = "10") @ApiParam(value = "当前页,默认为10", required = true)Integer pageSize) {
        PageInfo info = sysMessageService.queryPage(entity,pageNum,pageSize);
        return APIResponse.returnSuccess(info);
    }


    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation(value = "消息详情", httpMethod = "GET")
    public APIResponse info(@ApiParam(value = "主键id", required = true)Long messageId) {
        SysMessageEntity sysMessage = sysMessageService.getById(messageId);
        if(sysMessage!=null)
            return APIResponse.returnSuccess(sysMessage);
        return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除消息", httpMethod = "POST")
    public APIResponse delete(@ApiParam(value = "主键id,参数类型:数组", required = true)@RequestBody Long[] messageIds) {
        if(sysMessageService.removeByIds(Arrays.asList(messageIds)))
            return APIResponse.returnSuccess();
        return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
    }

    /**
     * 已读
     */
    @PostMapping("/read")
    @ApiOperation(value = "设置已读", httpMethod = "POST")
    public APIResponse readByIds(@ApiParam(value = "主键id,参数类型:数组", required = true)@RequestBody Long[] messageIds) {
        List<SysMessageEntity> list = new ArrayList<>();
        for(Long id : messageIds){
            SysMessageEntity entity = new SysMessageEntity();
            entity.setMessageId(id);
            entity.setReadFlag(1);
            entity.setReadTime(new Date());
            list.add(entity);
        }
        if(sysMessageService.updateBatchById(list)){
            return APIResponse.returnSuccess();
        }
        return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
    }

}
